/*
 @description: 寄件

 @author: 李建翰

 @update: 2021年9月7日14:44:27
*/
'use strict'
const Controller = require('egg').Controller;
class MailCpntroller extends Controller{
   
    async index() { //搜索
        const {ctx,app} = this;
        const {keyname,page=1,number=8} = ctx.request.query
        const offset=(page-1)*number
        let data,count
        if(keyname && page){
            data = await app.mysql.query(`select id,order_mail,name_mail,phone_mail,address_mail,	jname_mail,jphone_mail,jaddress_mail,time_mail,ctime_mail from fuxiao_mail where order_mail like '%${keyname}%' limit ${offset},${number}`)
            count=await app.mysql.query(`select count(*) as count from fuxiao_mail where order_mail like '%${keyname}%'`)
        }else if(keyname){
            data = await app.mysql.query(`select id,order_mail,name_mail,phone_mail,address_mail,jname_mail,jphone_mail from fuxiao_mail,jaddress_mail,time_mail,ctime_mail where order_mail ='%${keyname}%' or name_mail like '%${keyname}%' limit ${offset},${number}`)
            count=await app.mysql.query(`select count(*) as count from fuxiao_mail where order_mail like '%${keyname}%' or name_mail like '%${keyname}%'`)
        }else{
            data = await app.mysql.query(`select id,order_mail,name_mail,phone_mail,address_mail,jname_mail,jphone_mail,jaddress_mail,time_mail,ctime_mail from fuxiao_mail limit ${offset},${number}`)
            count=await app.mysql.query(`select count(*) as count from fuxiao_mail`)

            
        }
        ctx.body = {
            code:0,
            msg:"查找成功",
            data,
            count:count[0].count
        }
    }

    async new(){//前端
        const {ctx,app} = this;
        const{order_mail} = ctx.request.query;
        const result=await app.mysql.query(`select * from fuxiao_mail `)
        if(result){
            const result1=result.filter(item=>item.order_mail==order_mail);
            // console.log(order_mail);
            if(result1.length!==0){
                ctx.body={ 
                    code:0,
                    msg:'查找成功',
                    data:result1
                };
            }else{
                ctx.body={
                        code:0,
                        msg:'找不到订单',
                        data:result1
                    };
            }
            // if(){

            // }
            // ctx.body={
            //     code:0,
            //     msg:'查找成功',
            //     data:result
            // };
        }else{
            ctx.body = {
                code:4007,
                msg:"查找失败"
            }
        }
    }

    async show(){
        const {ctx,app} = this;
        const {id} = ctx.params;
        const result=await app.mysql.get('fuxiao_mail',{ID:id})
        if(result){
            ctx.body={
                code:0,
                msg:'查找成功',
                data:result
            };
        }else{
            ctx.body = {
                code:4007,
                msg:"查找失败"
            }
        }
    }
    // 添加
    async create(){     //post
        const { ctx,app} = this;
        const {name_mail,phone_mail,address_mail,jname_mail,jphone_mail,jaddress_mail,time_mail}=ctx.request.body
        const phone = /^1[3-9][0-9]{9}$/;
        let phone1=phone.test(phone_mail);
        let phone2=phone.test(jphone_mail);

        if(phone1 && phone_mail && address_mail && time_mail && jname_mail && phone2 && jaddress_mail){
            const data=await app.mysql.insert('fuxiao_mail',{order_mail:'LWL'+Date.now(),name_mail,phone_mail,address_mail,jname_mail,jphone_mail,jaddress_mail,time_mail:new Date(time_mail).getTime(),ctime_mail:Date.now()});
            const result=await app.mysql.query(`select MAX(order_mail) as order_mail  from fuxiao_mail `)


            ctx.body = {
                code: 0,
                msg: "新增成功",
                result1:result[0].order_mail
            };
        } else {
            ctx.body = {
                code: 4006,
                msg: "添加失败"
            };
        }
    }

    
    //改
    async update(){
        const {ctx,app} =this;
        const {name_mail,phone_mail,address_mail,jname_mail,jphone_mail,jaddress_mail,time_mail} = ctx.request.body
        const {id} = ctx.params;
        const phone = /^1[3-9][0-9]{9}$/;
        let phone1=phone.test(phone_mail);
        let phone2=phone.test(jphone_mail);
        const row={
            order_mail:'LWL'+Date.now(),
            name_mail:name_mail,
            phone_mail:phone_mail,
            address_mail:address_mail,
            jname_mail:jname_mail,
            jphone_mail:jphone_mail,
            time_mail:new Date().getTime(),
            jaddress_mail:jaddress_mail,
            ctime_mail:Date.now()
        };
        const options={
            where:{
                id:+id
            }
        };
        if(phone1 && phone2){
            const result=await app.mysql.update('fuxiao_mail',row,options);
            if(result){
                ctx.body={
                    code:0,
                    msg:"编辑成功",
                };
            }else{
                ctx.body={
                    code:4008,
                    msg:"编辑失败",
                };
            } 
        }else{
            ctx.body={
                code:4008,
                msg:"电话号码输入格式错误",
            };
        }
        
            
    }
    //删
    async destroy() { //DELETE
        const {ctx,app} = this;
        const {id} = ctx.params;
        const result =await app.mysql.delete(`fuxiao_mail`,{id:+id})
        if(result){
            ctx.body = {
                code: 0,
                msg: "删除成功",
            };
        }else{
            ctx.body = {
                code: 4007,
                msg: "删除失败",
            }; 
        }
    }
}
module.exports = MailCpntroller